package com.qire.manhua.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.qire.manhua.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 自适应平铺 {@code RecyclerView} ：
 * <p>
 *     在指定 {@code RecyclerView} 宽度内，可根据子项宽度自适配间隔宽度，或者根据间距宽度自适配子控件宽度。
 * </p>
 */
public class AdaptiveRecyclerView extends RecyclerView {

    public AdaptiveRecyclerView(@NonNull Context context) {
        this(context, null);
    }

    public AdaptiveRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.recyclerViewStyle);
    }

    public AdaptiveRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        post(() -> addItemDecoration(new AdaptiveItemDecoration()));
    }

    public class AdaptiveItemDecoration extends ItemDecoration {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
            LayoutManager layoutManager = getLayoutManager();
            if(layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                int spanCount = gridLayoutManager.getSpanCount();
                gridLayoutItemOffsets(spanCount, outRect, view, parent, state);
            } else {
                int spanCount = state.getItemCount();
                linearLayoutItemOffsets(spanCount, outRect, view, parent, state);
            }
        }

        private void gridLayoutItemOffsets(int spanCount, Rect outRect, View view, RecyclerView parent, State state) {
            int parentWidth = parent.getWidth();
            int childWidth = view.getLayoutParams().width;

            int spanWidth = (parentWidth / spanCount) - childWidth;

            int width = spanWidth / (spanCount - 1);

            int position = parent.getChildAdapterPosition(view) % spanCount;
            int left, top = 0, right, bottom = 0;

            // gridLayout 需要分别处理两端项目，因为每个项目的空间已被确定，偏移是相对网格单元项内的。
            if(position == 0) {
                left  = 0;
                right = 0;
            } else if(position == (spanCount - 1)) {
                left  = width;
                right = 0;
            } else {
                left  = width / 2;
                right = width / 2;
            }

            outRect.set(left, top, right, bottom);
        }

        private void linearLayoutItemOffsets(int spanCount, Rect outRect, View view, RecyclerView parent, State state) {
            int parentWidth = parent.getWidth();
            int childWidth = view.getLayoutParams().width;
            int spanWidth = (parentWidth / spanCount) - childWidth;
            int width = spanWidth / (spanCount - 1);
            // 只为边缘项加一半距，以避免两项之间有两倍的空格
            int position = parent.getChildAdapterPosition(view) % spanCount;
            int left, top = 0, right, bottom = 0;

            left  = position == 0 ? 0 : width / 2;
            right = position == (spanCount - 1) ? 0 : width / 2;

            outRect.set(left, top, right, bottom);
        }

    }

}
